package com.blockmeta.bbs.businesslibrary.base.fragment;


/**
 * @author AirThor
 * @date 2020/4/21.
 * 网络请求懒加载 主要用于viewpager承载Fragment
 */
public abstract class BaseLazyFragment extends BaseFragment {
    /**
     * 是否初始化布局
     */
    protected boolean mIsViewInitiated;
    /**
     * 当前界面是否可见
     */
    protected boolean mIsVisibleToUser;

    @Override
    public void initView() {
        initializeLayout();
        mIsViewInitiated = true;
        isCanLoadData();

    }


    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        this.mIsVisibleToUser = isVisibleToUser;
        isCanLoadData();
    }

    private void isCanLoadData() {
        if (mIsViewInitiated && mIsVisibleToUser){
            netWork();
            // 加载过数据后，将isViewInitiated和isVisibleToUser设置成false，防止重复加载数据
            mIsViewInitiated = false;
            mIsVisibleToUser = false;
        }
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }



    /**
     * 初始化控件
     */
    protected abstract void initializeLayout();

    /**
     * 网络请求
     */
    protected abstract void netWork();

}
